package com.endpoint.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.endpoint.book.dto.BookChapterAboutRespDto;
import com.endpoint.book.dto.BookContentAboutRespDto;
import com.endpoint.book.dto.BookRespDto;
import com.endpoint.book.entity.Book;
import com.endpoint.book.entity.BookContent;
import com.endpoint.book.entity.BookIndex;
import com.endpoint.book.feign.AuthorFeignService;
import com.endpoint.book.feign.MemberFeignService;
import com.endpoint.book.mapper.BookContentMapper;
import com.endpoint.book.service.BookContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.endpoint.book.service.BookIndexService;
import com.endpoint.book.service.BookService;
import com.endpoint.book.to.AuthorTo;
import com.endpoint.book.to.MemberBuyRecordTo;
import com.endpoint.common.constant.ResponseStatus;
import com.endpoint.common.exception.BusinessException;
import com.endpoint.common.utils.JwtTokenUtil;
import com.endpoint.common.utils.ResultBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 小说内容表 服务实现类
 * </p>
 *
 * @author Jack_Li
 * @since 2024-07-03
 */
@Service
public class BookContentServiceImpl extends ServiceImpl<BookContentMapper, BookContent> implements BookContentService {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookContentService bookContentService;

    @Autowired
    private BookIndexService bookIndexService;

    @Autowired
    private MemberFeignService memberFeignService;

    @Autowired
    private AuthorFeignService authorFeignService;

    @Override
    public ResultBean<BookContentAboutRespDto> getBookContentAbout(Long chapterId, String token) {
        BookContentAboutRespDto bookContentAboutRespDto = new BookContentAboutRespDto();

        //1.先查询章节信息  可以获取bookId
        BookIndex bookIndex = bookIndexService.getById(chapterId);
        BookChapterAboutRespDto bookChapterAboutRespDto = new BookChapterAboutRespDto();
        BeanUtils.copyProperties(bookIndex,bookChapterAboutRespDto);
        bookContentAboutRespDto.setChapterInfo(bookChapterAboutRespDto);

        //2.查询章节内容
        BookContent bookContent = bookContentService.getOne(new QueryWrapper<BookContent>().eq("index_id", chapterId));
        bookContentAboutRespDto.setBookContent(bookContent.getContent());

        //3.询小说信息
        Book book = bookService.getById(bookIndex.getBookId());
        BookRespDto bookRespDto = new BookRespDto();
        BeanUtils.copyProperties(book,bookRespDto);
        bookContentAboutRespDto.setBookInfo(bookRespDto);

        //4. 判断用户是否需要进行购买
        Boolean needBuy = false;//设置默认不需要购买

        if( bookIndex.getIsVip() != null && bookIndex.getIsVip() == 1 ){
            //4.1判断用户是否登录 ,未登录显示需要购买
            if(StringUtils.isEmpty(token) || JwtTokenUtil.isTokenExpired(token)){
                needBuy = true;
            }else{//已登录用户
                //4.2 判断是否购买过
                Long memberId = JwtTokenUtil.getUserIdFromToken(token);
                ResultBean<MemberBuyRecordTo> result = memberFeignService.getBuyRecordBymemberIdAndIndexId(memberId, chapterId);
                if(result != null && result.getCode() == 200){
                    MemberBuyRecordTo memberBuyRecordTo = result.getData();
                    if(memberBuyRecordTo == null){ //查询不到记录
                        needBuy = true;//需要购买
                    }
                }
                //4.3 判断是否为作者本人查看
                ResultBean<AuthorTo> authorResult = authorFeignService.getAuthorByMemberId(memberId);
                if(authorResult != null && authorResult.getCode() == 200){
                    AuthorTo authorTo = authorResult.getData();
                    //说明该会员绑定过 作者
                    if(authorTo != null && book.getAuthorId().equals(authorTo.getId())){
                        //该小说作者id与 该会员的作者ID 相等  不需要购买
                        needBuy =false;
                    }

                }
            }
        }
        bookContentAboutRespDto.setNeedBuy(needBuy);

        return ResultBean.ok(bookContentAboutRespDto);
    }

    @Override
    public ResultBean<BookContentAboutRespDto> getBookNextChapterContent(Long chapterId) {
        BookIndex bookIndex = bookIndexService.getById(chapterId);
        int nextIndex = bookIndex.getIndexNum() + 1;

        BookRespDto book = bookService.getBookById(bookIndex.getBookId());
        BookIndex end = bookIndexService.getById(book.getLastIndexId());
        BookIndex nextChapterIndex = bookIndexService.getOne(Wrappers.<BookIndex>lambdaQuery()
                .eq(BookIndex::getBookId, bookIndex.getBookId())
                .eq(nextIndex <= end.getIndexNum(), BookIndex::getIndexNum, nextIndex));

        BookContent bookContent = bookContentService.getOne(Wrappers.<BookContent>lambdaQuery()
                .eq(BookContent::getIndexId, nextChapterIndex.getId()));
        BookChapterAboutRespDto bookChapterAboutRespDto = new BookChapterAboutRespDto();
        BeanUtils.copyProperties(nextChapterIndex,bookChapterAboutRespDto);
        BookContentAboutRespDto bookContentAboutRespDto = new BookContentAboutRespDto();
        bookContentAboutRespDto.setBookContent(bookContent.getContent());
        bookContentAboutRespDto.setChapterInfo(bookChapterAboutRespDto);

        return ResultBean.ok(bookContentAboutRespDto);
    }

    @Override
    public ResultBean<BookContentAboutRespDto> getBookPreChapterContent(Long chapterId) {
        BookIndex bookIndex = bookIndexService.getById(chapterId);
        if (bookIndex.getIndexNum() > 0) {
            int preIndex = bookIndex.getIndexNum() - 1;
            BookRespDto book = bookService.getBookById(bookIndex.getBookId());
            BookIndex start = bookIndexService.getById(book.getFirstChapterId());
            BookIndex preChapterIndex = bookIndexService.getOne(Wrappers.<BookIndex>lambdaQuery()
                    .eq(BookIndex::getBookId, bookIndex.getBookId())
                    .eq(preIndex >= start.getIndexNum(), BookIndex::getIndexNum, preIndex));
            BookContent bookContent = bookContentService.getOne(Wrappers.<BookContent>lambdaQuery()
                    .eq(BookContent::getIndexId, preChapterIndex.getId()));
            if (bookContent == null) {
                throw new BusinessException(ResponseStatus.PARAM_ERROR);
            }
            BookChapterAboutRespDto bookChapterAboutRespDto = new BookChapterAboutRespDto();
            BeanUtils.copyProperties(preChapterIndex,bookChapterAboutRespDto);
            BookContentAboutRespDto bookContentAboutRespDto = new BookContentAboutRespDto();
            bookContentAboutRespDto.setBookContent(bookContent.getContent());
            bookContentAboutRespDto.setChapterInfo(bookChapterAboutRespDto);
            return ResultBean.ok(bookContentAboutRespDto);
        }
        throw new BusinessException(ResponseStatus.PAGE_ERROR);
    }

}
